package com.dev.happy.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * 请求统一返回对象
 * @param <E> 成功数据的实体
 * @author caonuoqi
 */
@ApiModel
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult<E> {

    /**
     * 成功
     */
    private static final int SUCCESS = 0;

    /**
     * 失败
     */
    private static final int FAILURE = 1;

    /**
     * 状态编码
     */
    @ApiModelProperty(value = "状态编码,0为成功，其他均为失败")
    private int code = SUCCESS;

    /**
     * 提示信息
     */
    @ApiModelProperty(value = "提示信息")
    private String message = "操作成功！";

    @ApiModelProperty(value = "返回对象")
    private E data;



    public CommonResult(E data) {
        this.data = data;
    }

    public CommonResult(int code, String message) {
        this(code, message, null);
    }

    /**
     * 操作成功，默认提示信息-操作成功
     */
    public static CommonResult success() {
        return new CommonResult(SUCCESS, "操作成功！");
    }

    /**
     * 操作成功，自定义提示消息
     *
     * @param msg 信息
     */
    public static CommonResult success(String msg) {
        return new CommonResult(SUCCESS, msg);
    }

    /**
     * 操作失败，默认提示信息-操作失败
     */
    public static CommonResult failure() {
        return new CommonResult(FAILURE, "操作失败！");
    }

    /**
     * 操作失败，自定义提示信息
     *
     * @param message 提示信息
     */
    public static CommonResult failure(String message) {
        return new CommonResult(FAILURE, message);
    }

    /**
     * 操作成功，并返回对象
     *
     * @param data 结果对象
     * @param <E>
     */
    public static <E> CommonResult<E> success(E data) {
        return new CommonResult(data);
    }


}